package com.hyperplan.neptune.parser.generic;

import java.util.List;

import com.hyperplan.neptune.lexer.Token;

public class TokenParser<T extends Token> implements Parser<T> {

	private Class<T> cls;
	
	public TokenParser(Class<T> cls) {
		this.cls = cls;
	}
	
	public ParserSolution<T> parse(List<Token> tokens, int index)
			throws ParserException {
		if (index < tokens.size()) {
			Token token = tokens.get(index);
			if (cls.isInstance(token)) {
				@SuppressWarnings("unchecked")
				T token2 = (T)token;
				return new ParserSolution<T>(token2, index + 1);
			} else {
				throw new ParserException(token);
			}
		} else {
			throw new ParserException();
		}
	}
}
